using Unity.Mathematics;

namespace DA.AnimGraph {
    internal struct Vector2Link {

        private readonly AUID name;
        private readonly LinkSourceType sourceType;
        public readonly bool useClamp;
        public readonly float2 clampRangeX;
        public readonly float2 clampRangeY;
        public float2 value { get; private set; }

        public Vector2Link(in Vector2LinkDesc desc) {
            name = desc.name;
            sourceType = desc.sourceType;
            useClamp = desc.useClamp;
            clampRangeX = desc.clampRangeX;
            clampRangeY = desc.clampRangeY;
            value = desc.defaultValue;
        }

        internal float2 Evaluate(in EvaluateContext evaluateContext, in GraphContext graphContext, ScopeContext scope) {
            if (scope.disableBlackboardUpdate) return value;
            if (sourceType == LinkSourceType.Const) return value;

            float2 newValue = value;

            switch (sourceType) {
                case LinkSourceType.Blackboard:
                    graphContext.blackboard.GetValue(name, ref newValue);
                    break;
            }

            if (useClamp) {
                newValue.x = math.clamp(newValue.x, clampRangeX.x, clampRangeX.y);
                newValue.y = math.clamp(newValue.y, clampRangeY.x, clampRangeY.y);
                value = newValue;
            } else {
                value = newValue;
            }
            return value;
        }
    }
}
